// **************************************************************
// Com ++ Framework by the C++ World
// Please report defects: mailto:compp@cplusplusworld.com
// Please visit: http://www.cplusplusworld.com/socketlibrary.html
// **************************************************************

#include "ComPlusPlus/DirEntry.h"
#include <string.h>



compp::DirEntry::DirEntry() {


}

void compp::DirEntry::setPath(const std::string & Path) {

  if ( 0 != stat( Path.c_str(), & StatStructure) ) {
    std::string s = "Cannot stat " ;
    s += Path;
    throw syspp::ComException ( s.c_str() );
  }
  
  this->Name = Path;

}

compp::DirEntry::DirEntry(const struct dirent & Entry) {


  if ( 0 != stat( Entry.d_name, & StatStructure) ) {
    std::string s = "Cannot stat " ;
    s += Entry.d_name;
    throw syspp::ComException ( s.c_str() );
  }

  Name = Entry.d_name;

}


compp::DirEntry::DirEntry ( const std::string & dName ) {

  if ( 0 != stat( dName.c_str(), & StatStructure) ) {
    std::string s = "Cannot stat " ;
    s += dName;
    throw syspp::ComException ( s.c_str() );
  }
  
  this->Name = dName;
}

compp::DirEntry::DirEntry ( const unsigned int Inode ) {

  throw syspp::ComException ( "Sot supported yet. Sorry." );

}


compp::DirEntry::~DirEntry() {




}

bool compp::DirEntry::lessName ( const compp::DirEntry & LeftEntry, const compp::DirEntry & RightEntry  )  {
  
  int i; 
  
  i = strcmp ( LeftEntry.getName().c_str(), RightEntry.getName().c_str() ) ;

  if ( i < 0 ) 
    return true;

  return false;

}


bool compp::DirEntry::greaterName ( const compp::DirEntry & LeftEntry, const compp::DirEntry & RightEntry  )  {
  
  int i; 
  
  i = strcmp ( LeftEntry.getName().c_str(), RightEntry.getName().c_str() ) ;

  if ( i > 0 ) 
    return true;

  return false;

}

const bool compp::DirEntry::isDirectory ()  const {

  if ( S_ISDIR (StatStructure.st_mode) ) 
    return true;

  return false;

}

const bool compp::DirEntry::isRegularFile() const {

  if ( S_ISREG (StatStructure.st_mode) ) 
    return true;

  return false;

}

const bool compp::DirEntry::isCharDevice () const {

  if ( S_ISCHR (StatStructure.st_mode) ) 
    return true; 

  return false;

}

const bool compp::DirEntry::isBlockDevice () const {

  if ( S_ISBLK (StatStructure.st_mode) ) 
    return true;

  return false;

}

const bool compp::DirEntry::isFIFO () const  {

  if ( S_ISFIFO (StatStructure.st_mode) ) 
    return true;

  return false;

}

const bool compp::DirEntry::isSocket () const {
#ifdef S_ISSOCK

  if ( S_ISSOCK (StatStructure.st_mode) ) 
    return true;
  return false;

#else
  return false;
#endif
}


const bool compp::DirEntry::isSymLink () const {
#ifdef S_ISLNK
  if ( S_ISLNK (StatStructure.st_mode) ) 
    return true;

  return false;
#else
 return false;
#endif


}

bool compp::DirEntry::Exist ( const std::string & Path ) {
  
  struct stat Ss;

  if ( 0 == stat( Path.c_str(), & Ss) ) 
    return true;

  return false;
  
}


const std::string compp::DirEntry::getName () const  {

  return Name;

}

const int compp::DirEntry::getInode () const {

  return StatStructure.st_ino;

}

const int compp::DirEntry::getAccessTime() const {

  return StatStructure.st_atime;
  
}

const int compp::DirEntry::getChangeTime()  const {

  return StatStructure.st_ctime;

}

const int compp::DirEntry::getLastModificationTime() const {

  return StatStructure.st_mtime;

}
 
const int compp::DirEntry::getSize() const {

  return StatStructure.st_size;

}

const int compp::DirEntry::getUid() const {

  return StatStructure.st_uid;

}

const int compp::DirEntry::getGid() const {

  return StatStructure.st_gid;

}
	

const int compp::DirEntry::getNumLinks() const {

  return StatStructure.st_nlink;

}


compp::File *compp::DirEntry::getFile () {

  return new File ( this->Name );

}

